﻿// ***********************************************************************
// Provided for Informational Purposes Only
//
// Apache 2.0 License
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 
//
// THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY 
// IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR 
// PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT.
//
// See the Apache Version 2.0 License for specific language governing
// permissions and limitations under the License.
// ***********************************************************************
// Assembly  : ContractMeow.WinRT
// File      : JsonDotNetDeserializer.cs
//
// ***********************************************************************
using Newtonsoft.Json;
using System;
using System.Text;

namespace ContractMeow.WinRT.Serialization
{
	/// <summary>
	/// Class JsonDotNetDeserializer
	/// </summary>
	public class JsonDotNetDeserializer : IDeserializer
	{
		/// <summary>
		/// Gets the text encoding.
		/// </summary>
		/// <value>The text encoding.</value>
		public Encoding TextEncoding { get; protected set; }

		/// <summary>
		/// Gets the deserializer settings.
		/// </summary>
		/// <value>The deserializer settings.</value>
		public JsonSerializerSettings DeserializerSettings { get; protected set; }

		/// <summary>
		/// Initializes a new instance of the <see cref="JsonDotNetDeserializer" /> class.
		/// </summary>
		/// <param name="encoding">The encoding.</param>
		public JsonDotNetDeserializer(Encoding encoding = null)
		{
			TextEncoding = encoding ?? Encoding.UTF8;

			DeserializerSettings = new JsonSerializerSettings()
				{
					NullValueHandling = NullValueHandling.Ignore
				};
		}

		/// <summary>
		/// Deserializes the specified content.
		/// </summary>
		/// <typeparam name="T"></typeparam>
		/// <param name="content">The content.</param>
		/// <returns>The deserialized object</returns>
		public T Deserialize<T>(String content)
		{
			return JsonConvert.DeserializeObject<T>(content, DeserializerSettings);
		}

		/// <summary>
		/// Deserializes the async.
		/// </summary>
		/// <typeparam name="T"></typeparam>
		/// <param name="content">The content.</param>
		/// <returns>The deserialized object</returns>
		public T Deserialize<T>(Byte[] content)
		{
			return Deserialize<T>(TextEncoding.GetString(content, 0, content.Length));
		}
	}
}